/**
 * Copyright Dinglian. All Rights Reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lyh.consumer.service;

/**
 * 类描述
 *
 * @author LinYeHui
 * @date 2022/5/26
 * @company Dinglian
 */

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * 基于此对象处理RemoteProviderService接口调用时出现的服务中断,超时等问题
 */
/**
 * Feign回调接口工厂对象,基于此对象创建Feign接口实现类对象.
 * 这个实现类对象,用于做容错处理?(有备无患,这里做熔断处理.)
 */
@Slf4j
@Component//该注解一定要加，不然会出现，ioc注入异常
public class ProviderFallbackFactory implements FallbackFactory<RemoteProviderService> {
    @Override
    public RemoteProviderService create(Throwable throwable) {
//        return new RemoteProviderService() {//匿名内部类
//            @Override
//            public String echoMessage(String msg) {
//                //...报警...(发短信,email,日志.....)
//                return "服务暂时不可用,稍等片刻再访问";
//            }
//        };
        //如上return语句的简单写法,可采用如下方式:
        return msg -> {//lambda表达式 (JDK8中的语法)
            //...报警...(发短信,email,日志.....)
            log.error("error: {} ", "sca-provider服务调用失败");
            return "服务暂时不可用,稍等片刻再访问";
        };
    }
}
